package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.bumptech.glide.Glide;

import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.HomeAppItemviewBinding;
import sz.szsmk.citizencard.databinding.HomeHorListViewBinding;
import sz.szsmk.citizencard.model.network.bean.response.HomeAllfunResponse;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * 设置横向滚动的列表
 */
public class HorizontalAdapter extends DelegateAdapter.Adapter<HorizontalAdapter.HorizontalViewHolder> {

    private static final int HORLIST_TYPE = 0x23;

    private Context mContext;

    private LayoutHelper mLayoutHelper;

    private int position = 0;

    private int xOffset = 0;

    private List<HomeAllfunResponse.FunctionListBean> listBeen;

    private OnItemClickListener mOnItemClickListener = null;
    private HomeAllfunResponse.FunctionListBean functionListBean;
    private final int itemWidth;

    private BaseInterface baseInterface;

    public interface BaseInterface {
        public void onClick(View view, String url, String syslabl, int need_header,int id,int modularId);
    }

    public void setBaseInterface(BaseInterface baseInterface) {
        this.baseInterface = baseInterface;
    }


    public HorizontalAdapter(Context context, LayoutHelper layoutHelper) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        int widthPixels = mContext.getResources().getDisplayMetrics().widthPixels;
        float dp5 = mContext.getResources().getDimension(R.dimen.dp_5);
        itemWidth = (int) ((widthPixels - dp5 * 2) / 5);

    }


    /**
     * 设置数据
     *
     * @param listBeen
     */
    public void setAppData(List<HomeAllfunResponse.FunctionListBean> listBeen) {
        this.listBeen = listBeen;
    }

    private void loadItemData(final int position, final HomeAppItemviewBinding binding) {
        if (listBeen != null) {
            functionListBean = listBeen.get(position);
            String icon_url = functionListBean.getIcon_url();
//            int function_id = functionListBean.getFunction_id();
            binding.homeAppItemTv.setText(functionListBean.getFunction_name());
            Glide.with(mContext)
                    .load(icon_url)
                    .centerCrop()
                    .into(binding.homeAppItemIv);
        }
        ViewGroup.LayoutParams layoutParams = binding.homeCardItem.getLayoutParams();
        layoutParams.width = itemWidth;
        binding.homeCardItem.setLayoutParams(layoutParams);
        binding.homeCardItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = listBeen.get(position).getLink_url();
                String sys_label = listBeen.get(position).getSys_label();
                int function_id = listBeen.get(position).getFunction_id();
                int need_header = listBeen.get(position).getNeed_header();
                int modularId = listBeen.get(position).getModularId();
                if (baseInterface != null) {
                    baseInterface.onClick(v, url, sys_label, need_header,function_id,modularId);
                }



            }
        });
    }

    private void toLogin() {
        JumpUtils.jump(mContext, "login", 0);
    }

//    构建页面

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    public HorizontalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == HORLIST_TYPE)
            return HorizontalViewHolder.creat(parent);
        else
            return null;
    }

    @Override
    public void onBindViewHolder(HorizontalViewHolder holder, int position) {
        if (holder.itemView instanceof LinearLayout) {
            RecyclerView recyclerView = (RecyclerView) holder.binding.appListView;
            GridLayoutManager layoutManager = new GridLayoutManager(mContext, 2);
            layoutManager.setOrientation(GridLayoutManager.HORIZONTAL);
            recyclerView.setLayoutManager(layoutManager);

            recyclerView.setAdapter(new RecyclerView.Adapter<ItemViewHolder>() {
                @Override
                public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                    return ItemViewHolder.creat(parent);
                }

                @Override
                public void onBindViewHolder(ItemViewHolder holder, int position) {
                    loadItemData(position, holder.binding);
                }

                @Override
                public int getItemCount() {
                    return listBeen == null ? 0 : listBeen.size();
                }
            });

            recyclerView.clearOnScrollListeners();

            if (listBeen != null && listBeen.size() > 10) {
                holder.binding.indexGroup.setVisibility(View.VISIBLE);
                recyclerView.addOnScrollListener(new OnRecyclerScrollListener(holder.binding.indexGroup));
            }
        }
    }

    /**
     * 指示器监听滚动
     */
    class OnRecyclerScrollListener extends RecyclerView.OnScrollListener {

        private RadioGroup indexGroup;

        public OnRecyclerScrollListener(RadioGroup indexGroup) {
            this.indexGroup = indexGroup;
        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (newState == RecyclerView.SCROLL_STATE_IDLE && !recyclerView.canScrollHorizontally(1))
                indexGroup.check(R.id.radioRight);
            else if (newState == RecyclerView.SCROLL_STATE_IDLE && !recyclerView.canScrollHorizontally(-1))
                indexGroup.check(R.id.radioLeft);
        }
    }

    @Override
    public void onViewDetachedFromWindow(HorizontalViewHolder holder) {
        if (holder.itemView instanceof RecyclerView) {
            RecyclerView recyclerView = ((RecyclerView) holder.binding.appListView);
            GridLayoutManager manager = (GridLayoutManager) recyclerView.getLayoutManager();
            position = manager.findFirstVisibleItemPosition();
            View view = manager.findViewByPosition(position);
            ViewGroup.MarginLayoutParams lp =
                    (ViewGroup.MarginLayoutParams) view.getLayoutParams();
            if (view != null) {
                xOffset = view.getLeft() - lp.leftMargin; //如果你设置了margin则减去
            }
        }
        super.onViewDetachedFromWindow(holder);
    }

    @Override
    public void onViewAttachedToWindow(HorizontalViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        if (holder.itemView instanceof RecyclerView) {
            RecyclerView recyclerView = ((RecyclerView) holder.binding.appListView);
            GridLayoutManager manager = (GridLayoutManager) recyclerView.getLayoutManager();
            manager.scrollToPositionWithOffset(position, xOffset);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return HORLIST_TYPE;
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    /**
     * 横向滚动Holder 容器
     */
    static class HorizontalViewHolder extends RecyclerView.ViewHolder {
        public HomeHorListViewBinding binding;

        public static HorizontalViewHolder creat(ViewGroup parent) {
            HomeHorListViewBinding binding = HomeHorListViewBinding
                    .inflate(LayoutInflater.from((parent.getContext())), parent, false);
            return new HorizontalViewHolder(binding);
        }

        public HorizontalViewHolder(HomeHorListViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    /**
     * item项的列表 子项
     */
    static class ItemViewHolder extends RecyclerView.ViewHolder {

        public HomeAppItemviewBinding binding;

        public static ItemViewHolder creat(ViewGroup parent) {
            HomeAppItemviewBinding binding = HomeAppItemviewBinding
                    .inflate(LayoutInflater.from((parent.getContext())), parent, false);
            return new ItemViewHolder(binding);
        }

        public ItemViewHolder(HomeAppItemviewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    public interface OnItemClickListener {
        void onClick(int i, View view);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }
}